package com.blogs.ui;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blogs.component.BaseActivity;
import com.blogs.component.OffDLChannelAdapter;
import com.blogs.entity.ChannelFeed;
import com.blogs.entity.ListDate;
import com.blogs.service.OffLineDLService;
import com.blogs.tools.DBSharedPreferences;
import com.cnblogs.R;

public class OfflineDownloadActivity extends BaseActivity implements OnItemClickListener,OnClickListener{
	private ArrayList<ChannelFeed> channelList;
	private DBSharedPreferences sp;
	private Button title_include_back;
	private TextView title_include_title;
	private Button off_line_down_btn;
	private RelativeLayout title_top_bg;
	
	private LinearLayout offlinedl_list_shade_bg;
	private ListView offlinedl_list_body;
	private OffDLChannelAdapter offDLChannelAdapter;
	private ProgressBar offline_download_pb;
	private Button offline_cancel_btn;
	private TextView offline_process_tv;
	
	private boolean[] bechecks;
	private ArrayList<String> dllist;
	private ArrayList<String> dllist_name;
	private OfflinePbReceiver offlinePbReceiver;
	int pagesize=1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.offline_download);
		sp= new DBSharedPreferences(this);
		ViewGroup headview=(ViewGroup)findViewById(R.id.main_title_include);
		title_top_bg=(RelativeLayout)headview;
		title_include_title=(TextView)headview.findViewById(R.id.title_include_title);
		title_include_back=(Button)headview.findViewById(R.id.title_include_back);
		off_line_down_btn=(Button)headview.findViewById(R.id.off_line_down_btn);
		title_include_title.setText("离线下载");
		title_include_back.setVisibility(View.VISIBLE);
		off_line_down_btn.setVisibility(View.VISIBLE);
		title_include_back.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		off_line_down_btn.setOnClickListener(this);
		offlinedl_list_shade_bg=(LinearLayout)findViewById(R.id.offlinedl_list_shade_bg);
		offlinedl_list_shade_bg.setOnClickListener(this);
		offline_download_pb=(ProgressBar)findViewById(R.id.offline_download_pb);
		offline_cancel_btn=(Button)findViewById(R.id.offline_cancel_btn);
		offline_cancel_btn.setOnClickListener(this);
		offline_process_tv=(TextView)findViewById(R.id.offline_process_tv);
		this.initList();
		this.InitSkin();
		dllist=new ArrayList<String>();
		dllist_name=new ArrayList<String>();
		
		offlinePbReceiver=new OfflinePbReceiver();//设置广播接收
		IntentFilter skinFilter = new IntentFilter();
        skinFilter.addAction("OfflinePbReceiver");
        registerReceiver(offlinePbReceiver, skinFilter);
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			offlinedl_list_body.getBackground().setLevel(2);
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			offlinedl_list_body.getBackground().setLevel(1);
		}
	}
	private void initList()
	{
		channelList=ListDate.GetChannelList();
		bechecks=new boolean[channelList.size()];
		offlinedl_list_body=(ListView)findViewById(R.id.offlinedl_list_body);
		offlinedl_list_body.setDivider(getResources().getDrawable(R.drawable.line_01));
		offlinedl_list_body.setCacheColorHint(Color.TRANSPARENT);
		offDLChannelAdapter=new OffDLChannelAdapter(this, channelList,bechecks);
		offlinedl_list_body.setAdapter(offDLChannelAdapter);
		offlinedl_list_body.setOnItemClickListener(this);
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		dllist.clear();
		dllist_name.clear();
		for(int i=0;i<bechecks.length;i++)
		{
			if(bechecks[i])
			{
				dllist.add(channelList.get(i).channel_url);
				dllist_name.add(channelList.get(i).channel_name);
			}
		}
		if(dllist.size()==0)
		{
			return;
		}
		if(v==off_line_down_btn)
		{
			final String[] lang = {"一页","二页","三页"} ;
            new AlertDialog.Builder(this)
            .setTitle("要下载页数")//标题
            .setSingleChoiceItems(lang,0,new DialogInterface.OnClickListener() {//设置条目
                public void onClick(DialogInterface dialog, int which) {//响应事件
                    // do something
                    //关闭对话框
//                    dialog.dismiss();
                	pagesize=which+1;
                }
            })
            .setPositiveButton("下载", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                	off_line_down_btn.setClickable(false);
                	offline_process_tv.setText("0%");
        			offline_download_pb.setProgress(0);
        			offlinedl_list_shade_bg.setVisibility(View.VISIBLE);
        			Intent intent=new Intent(OfflineDownloadActivity.this,OffLineDLService.class);
        			intent.putExtra("dllist", dllist);
        			intent.putExtra("dllist_name", dllist_name);
        			intent.putExtra("channelsize", dllist.size());
        			intent.putExtra("pagesize", pagesize);
        			startService(intent);
                }
            })//添加button并响应点击事件
            .show();

		}
		else if(v==offline_cancel_btn)
		{
			Intent intent=new Intent(this,OffLineDLService.class);
			intent.putExtra("stop", 1);
			startService(intent);
			offlinedl_list_shade_bg.setVisibility(View.GONE);
		}
	}
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		bechecks[position]=!bechecks[position];
		offDLChannelAdapter.notifyDataSetChanged();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(offlinePbReceiver);  
	}
	class OfflinePbReceiver extends BroadcastReceiver
	{
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if(intent.getIntExtra("finish", 0)==1)
			{
				offlinedl_list_shade_bg.setVisibility(View.GONE);
				off_line_down_btn.setClickable(true);
				offDLChannelAdapter.notifyDataSetChanged();
			}
			else
			{
				offlinedl_list_shade_bg.setVisibility(View.VISIBLE);
			}
			offline_download_pb.setProgress(intent.getIntExtra("progress", 0));
			offline_process_tv.setText(intent.getIntExtra("progress", 0)+"%");
		}
	}
}
